<template>
  <div class="my">
    <!-- 我的信息 -->
    <van-cell-group v-if="user" class="my-info">
      <!-- 用户资料 -->
      <van-cell class="user-info" center :border="false">
        <van-image
          class="avatar"
          slot="icon"
          round
          fit="cover"
          :src="currentUser.photo"
        />
        <div class="name" slot="title">{{ currentUser.name }}</div>
        <van-button class="update-btn" size="small" round>编辑资料</van-button>
      </van-cell>
      <!-- 用户基本信息 -->
      <van-grid :border="false" class="base-info">
        <van-grid-item class="base-info-item">
          <div class="text-wrap" slot="text">
            <div class="count">{{ currentUser.art_count }}</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="base-info-item">
          <div class="text-wrap" slot="text">
            <div class="count">{{ currentUser.follow_count }}</div>
            <div class="text">关注</div>
          </div>
        </van-grid-item>
        <van-grid-item class="base-info-item">
          <div class="text-wrap" slot="text">
            <div class="count">{{ currentUser.fans_count }}</div>
            <div class="text">粉丝</div>
          </div>
        </van-grid-item>
        <van-grid-item class="base-info-item">
          <div class="text-wrap" slot="text">
            <div class="count">{{ currentUser.like_count }}</div>
            <div class="text">获赞</div>
          </div>
        </van-grid-item>
      </van-grid>
    </van-cell-group>

    <div v-else class="not-login">
      <div class="mobile" @click="$router.push('/login')">
        <img src="./not-login.png" alt="" />
      </div>
      <div class="text">登录 / 注册</div>
    </div>

    <!-- 收藏和历史 -->
    <van-grid class="nav-grid mb-8" :column-num="2">
      <van-grid-item
        class="nav-grid-item"
        icon-prefix="icon"
        icon="shoucang"
        text="收藏"
      />
      <van-grid-item
        class="nav-grid-item"
        icon-prefix="icon"
        icon="lishi"
        text="历史"
      />
    </van-grid>

    <!-- 页面导航 -->
    <van-cell title="消息通知" is-link to="/" />
    <van-cell class="mb-8" title="小智同学" is-link to="/" />

    <!-- vuex 获取到的 user -->
    <van-cell v-if="user" class="logout" title="退出登录" @click="onLogout" />
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getCurrentUser } from "@/api/user";

export default {
  name: "my",
  data() {
    return {
      currentUser: {}, // 当前登录用户信息
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  created() {
    this.loadCurrentUser();
  },
  methods: {
    async loadCurrentUser() {
      const { data } = await getCurrentUser();
      this.currentUser = data.data;
    },
    onLogout() {
      this.$dialog
        .confirm({
          title: "退出提示",
          message: "确认退出吗？",
        })
        .then(() => {
          // 确认
          // 清除用户登录状态
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          // 取消
          // on cancel
        });
    },
  },
};
</script>

<style scoped lang="less">
/**
  有作用域的组件 默认只能作用到子组件的根节点
  要想作用得"更深" 
  /deep/ 深度作用操作符  还可以写成 >>> 或 ::v-deep, 
  加了样式才能作用到 子组件里的标签
*/
.my {
  .my-info {
    background: url("./banner.png") no-repeat;
    background-size: cover;
    .user-info {
      box-sizing: border-box;
      height: 239px;
      background-color: unset;
      padding-top: 76px;
      padding-bottom: 22px;
      .avatar {
        box-sizing: border-box;
        border: 2px solid #fff;
        width: 132px;
        height: 132px;
        margin-right: 22px;
      }
      .name {
        font-size: 40px;
        color: #fff;
      }
      .update-btn {
        height: 50px;
        font-size: 21px;
        color: #666;
      }
    }
    .base-info {
      .base-info-item {
        height: 130px;
        color: #fff;
        .text-wrap {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .count {
            font-size: 36px;
          }
          .text {
            font-size: 22px;
          }
        }
      }
      /deep/ .van-grid-item__content {
        background-color: unset;
      }
    }
  }

  .not-login {
    height: 360px;
    background: url("./banner.png") no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile {
      width: 122px;
      height: 122px;
      margin-bottom: 20px;
      img {
        width: 100%;
      }
    }
    .text {
      color: #fff;
      font-size: 28px;
    }
  }

  .nav-grid {
    .nav-grid-item {
      height: 140px;
      /deep/ .icon {
        font-size: 44px;
      }
      /deep/ .icon-shoucang {
        color: #fe655e;
      }
      /deep/ .icon-lishi {
        color: #ec8d10;
      }
      /deep/ .van-grid-item__text {
        font-size: 26px;
        color: #333333;
      }
    }
  }

  .logout {
    text-align: center;
    color: #ea6d6a;
    font-size: 30px;
  }
  .mb-8 {
    margin-bottom: 8px;
  }
}
</style>